#!/usr/local/python3/bin/python3
import os
import requests as req
from bs4 import BeautifulSoup

googlePatentBaseUrl = 'https://patents.google.com/patent/'

class Patent:
	def __init__(self, publishDate, patentType, appNo, publishNo):
		self.publishDate = publishDate
		self.patentType = patentType
		self.appNo = appNo
		self.publishNo = publishNo
	def getDir(self):
		return self.publishDate[:4] + "/" + self.publishDate.replace('-', '.')

def mkdir(path):
    path = path.strip()
    path = path.rstrip("\\")
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path) 
        return True
    else:
        return False
		
def downloadPdf(patent):
	res = req.get(googlePatentBaseUrl + patent.publishNo)
	doc = BeautifulSoup(res.text, "html.parser")
	pdfUrl = doc.find("meta", attrs={'name': "citation_pdf_url"})['content']
	pdfFileResponse =  req.get(pdfUrl)
	saveUrl = ""
	if patent.patentType == "发明公布":
		saveUrl = "/opt/fmgb/" + patent.getDir() + "/"
	if patent.patentType == "发明授权":
		saveUrl = "/opt/fmsq/" + patent.getDir() + "/"
	if patent.patentType == "实用新型":
		saveUrl = "/opt/syxx/" + patent.getDir() + "/"
	mkdir(saveUrl)
	with open(saveUrl + patent.appNo + ".pdf", "wb") as outputPdfFile:
		for chunk in pdfFileResponse.iter_content(100000):
			outputPdfFile.write(chunk)		

def record(line):
	with open("/opt/2016_no_pdf_record.txt", 'a') as f:
		f.write(line+"\n")

print("处理开始")
file = open("/opt/2016_no_pdf.txt")
line = file.readline()
sum = 0
downloadSum = 0
fmgbNum = 0
fmsqNum = 0
syxxNum = 0
while line:
	try:
		str = line.strip('\n').split(',')
		patent = Patent(str[3], str[2], str[0], str[1])
		downloadPdf(patent)
		print(patent.appNo+"下载成功")
		downloadSum = downloadSum + 1
		if patent.patentType == "发明公布":
			fmgbNum = fmgbNum + 1
		if patent.patentType == "发明授权":
			fmsqNum = fmsqNum + 1
		if patent.patentType == "实用新型":
			syxxNum = syxxNum + 1
	except BaseException:
		print('发生错误:'+line)
		record(line)
	finally:
		sum = sum + 1
		line = file.readline()
print("处理结束")
print("总数量:",sum)
print("已下载数量:",downloadSum)
print("发明公布数量:",fmgbNum)
print("发明授权总数量:",fmsqNum)
print("实用新型数量:",syxxNum)
